<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<%@include file="../../commons/meta.jsp"%>
<c:set var="baseUrl" value="${ctx}api/university-majors/" scope="request" />
<link rel="stylesheet" href="${ctx}assets/plugins/pure/pure-min.css" media="all" />
<script type="text/javascript">
  function validateForm() {

    if (empty($("#majorName").val())) {
      alertError('请输入专业名称！');
      return false;
    }

    if (empty($("#majorClass").val())) {
      alertError('请选择专业分类！');
      return false;
    }
    $("#forms").submit();
  }
  //  关闭弹出框 
  jQuery(document).ready(function() {
    $("#back_btn").click(function() {
      window.location.href = "${baseUrl}"
    });
  });
</script>
</head>
<body>
    <%@include file="../../commons/head.jsp"%>
    <div class="content">
        <%@include file="../../commons/menu.jsp"%>
        <!--右侧展示部分-->
        <div class="right-zq">
            <!--center content-->
            <div class="dynamic">
                <div class="role dynamiclayer" style="display: block;">
                    <div class="title orange2">
                        <span class="left">高校信息管理</span> <label> > </label> <span>编辑学校专业</span>
                    </div>
                    <form class="" id="forms" method="post" action="${baseUrl}update">
                        <div class="role dynamiclayer" style="display: block;">
                            <div class="news-table">
                                <table>
                                    <tr>
                                        <td class="item">学校名称:<span class="error">*</span></td>
                                        <td width="60%">
                                            <input type="hidden" id="id" name="id" value="${obj.id}" />
                                            <input class="p100" id="universityName" name="universityName" type="text" placeholder="" value="${obj.universityName}" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="item">专业层次:</td>
                                        <td width="60%">
                                            <select name="searchMap.majorLevel,eq,int" id="majorLevel" class="input1 p100" style="text-align: left;">
                                                <option value="0">请选择</option>
                                                <option value="1">本科</option>
                                                <option value="2">专科</option>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="item">学科:</td>
                                        <td width="60%">
                                            <select id="majorSubject" name="searchMap.majorSubject,eq,string" class="input1 p100" style="text-align: left;" onchange="getMajorClass()">
                                                <option code="0" value="">请选择</option>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="item">专业类:</td>
                                        <td width="60%">
                                            <select id="majorClass" name="searchMap.majorClass,eq,string" class="input1 p100" style="text-align: left;" onchange="getMajors()">
                                                <option value="0">请选择</option>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="item">专业:</td>
                                        <td width="60%">
                                            <input type="hidden" id="majorCode" name="majorCode" value="${obj.majorCode}" />
                                            <select id="majorName" name="majorName" class="input1 p100" style="text-align: left;" onchange="setMajorCode()">
                                                <option value="">请选择</option>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td></td>
                                        <td>
                                            <button class="orange2" type="button" id="add_btn" onclick="validateForm()">保存&nbsp;<i class="fa fa-floppy-o"></i>
                                            </button> &nbsp;&nbsp;
                                            <button class="orange2" type="button" id="back_btn">返回&nbsp;<i class="fa fa-undo"></i>
                                            </button>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <!--center content over-->
        </div>
    </div>
    <%@include file="../../commons/footpop.jsp"%>
    <!-- 页尾，提示消息 -->
    <jsp:include page="../../commons/foot_message.jsp"></jsp:include>
    <script type="text/javascript">
          // 定义学科代码
          var selectedSubjectCode = "";
          var defalutMajor;
          var isMajorFirst = true;
          var isMajorClassFirst = true;
          //初始化时加载学科下拉框数据
          function getMajorSubject() {
            // 默认按id排序
            var query = {};
            // 学科的父级代码默认为"00"
            query.parentCode = "00";
            var searchJsonUrl = 'api/major-types/findAll';
            var param = JSON.stringify(query);

            $.ajax({
                  url : searchJsonUrl,
                  data : param,
                  type : 'POST',
                  contentType : "application/json",
                  dataType : 'json',
                  success : function(result) {
                    debugs(result);
                    var list = result.list;
                    if (list.length > 0) {
                      var option = "<option value='0'>请选择</option>";
                      $
                          .each(
                              list,
                              function(index, item) {
                                option += "<option code='" + item.majorTypeCode + "' value='" + item.majorTypeName + "'>"
                                    + item.majorTypeName + "</option>";
                              });
                      $("#majorSubject").html(option);
                    }
                  },
                  error : function(xhr) {
                    alertError("系统异常，请联系管理员！");// 请求失败遇到异常触发
                  }
                });
          }

          /**
           * 获取专业类
           * @returns
           */
          function getMajorClass() {
            var parentCode = $('#majorSubject option:selected').attr("code");

            if (empty(parentCode)) {
              document.getElementById("majorClass").value = "";
              var option = "<option value='0'>请选择</option>";
              $("#majorClass").html(option);
              getMajors();
              return;
            }
            debugs(parentCode);

            // 默认按id排序
            var query = {};
            query.parentCode = parentCode;
            var searchJsonUrl = 'api/major-types/findAll';
            var param = JSON.stringify(query);

            $.ajax({
                  url : searchJsonUrl,
                  data : param,
                  type : 'POST',
                  contentType : "application/json",
                  dataType : 'json',
                  success : function(result) {
                    debugs(result);
                    var list = result.list;
                    if (list.length > 0) {
                      var option = "<option value='0'>请选择</option>";
                      $
                          .each(
                              list,
                              function(index, item) {
                                option += "<option code='" + item.majorTypeCode + "' value='" + item.majorTypeName + "'>"
                                    + item.majorTypeName + "</option>";
                              });
                      $("#majorClass").html(option);
                    }

                    if (isMajorClassFirst) {
                      isMajorClassFirst = false;
                      document.getElementById("majorClass").value = defalutMajor.majorClass;
                      getMajors();
                    } else {
                      getMajors();
                    }
                  },
                  error : function(xhr) {
                    alertError("系统异常，请联系管理员！");// 请求失败遇到异常触发
                  }
                });
          }

          function getMajors() {
            var majorLevel = $('#majorLevel option:selected').attr("value");
            var majorSubject = $('#majorSubject option:selected').attr("value");
            var majorClass = $('#majorClass option:selected').attr("value");
            if ("0" == majorClass) {
              document.getElementById("majorName").value = "";
              var option = "<option value='0'>请选择</option>";
              $("#majorName").html(option);
              return;
            }
            // 默认按id排序
            var query = {};
            if ("0" != majorLevel) {
              query.level = majorLevel;
            }

            query.majorSubject = majorSubject;
            query.majorClass = majorClass;

            var searchJsonUrl = 'api/majors/getMajors';
            var param = JSON.stringify(query);

            $.ajax({
                  url : searchJsonUrl,
                  data : param,
                  type : 'POST',
                  contentType : "application/json",
                  dataType : 'json',
                  success : function(result) {
                    debugs(result);
                    var list = result;
                    if (list.length > 0) {
                      var option = "<option value='0'>请选择</option>";
                      $
                          .each(
                              list,
                              function(index, item) {
                                option += "<option code='" + item.majorCode + "' value='" + item.majorName + "'>"
                                    + item.majorName + "</option>";
                              });
                      $("#majorName").html(option);
                    }

                    if (isMajorFirst) {
                      isMajorFirst = false;
                      document.getElementById("majorName").value = defalutMajor.majorName;
                    }
                  },
                  error : function(xhr) {
                    alert(JSON.stringify(xhr));
                    alertError("系统异常，请联系管理员！");// 请求失败遇到异常触发
                  }
                });
          }

          function initDefault() {
            var query = {};
            query.majorCode = '${obj.majorCode}';
            var searchJsonUrl = '${ctx}api/majors/getMajorByCode';
            var param = JSON.stringify(query);

            $.ajax({
                  url : searchJsonUrl,
                  data : param,
                  type : 'POST',
                  contentType : "application/json",
                  dataType : 'json',
                  success : function(result) {
                    defalutMajor = result;
                    debugs(result);
                    document.getElementById("majorLevel").value = defalutMajor.majorLevel;
                    document.getElementById("majorSubject").value = defalutMajor.majorSubject;
                    getMajorClass(defalutMajor.majorSubjectCode);

                  },
                  error : function(xhr) {
                    alertError("系统异常，请联系管理员！");// 请求失败遇到异常触发
                  }
                });
          }

          function setMajorCode() {
            var majorName = document.getElementById("majorName");
            var selected = majorName.selectedIndex;
            document.getElementById("majorCode").value = majorName.options[selected]
                .getAttribute("code");//document.getElementById("majorName").code;

          }
          function initData() {
            getMajorSubject();
          }
          window.ready = initData();
          window.onload = initDefault();
        </script>
</body>
</html>
